/**
 * 判断str与pattern是否匹配
 * ?表示一个字符，*表示0-n个字符串的匹配方法
 * @param str 匹配字符串
 * @param pattern 匹配规则
 * @return 是否匹配
 */
const checkMatches = (str: string, pattern: string) => {
    if (null == pattern || null == str) {
        return str == pattern;
    }
    // 没有?号和*号，直接判断两者相等
    if (pattern.indexOf("?") === -1 && pattern.indexOf("*") === -1) {
        return str === pattern;
    }
    // 先判断没有*号的?号情况
    if (pattern.indexOf("*") == -1) {
        if (str.length != pattern.length) {
            return false;
        }
        let c1;
        let c2;
        for (let i = 0;i < str.length;i ++) {
            c1 = str[i];
            c2 = pattern[i];
            if (c1 != c2 && c2 != '?') {
                return false;
            }
        }
        return true;
    }
    // 有*号的
    let c1;
    let c2;
    let pIndex = 0;
    for (let i = 0; i < str.length; i++) {
        if (pIndex > pattern.length - 1) {
            return false;
        }
        c1 = str.charAt(i);
        c2 = pattern.charAt(pIndex);
        if (c1 != c2) {
            if (c2 == '?') {
                pIndex++;
                continue;
            } else if (c2 == '*') {
                // 如果已经是最后了，那么成功
                if (pIndex == pattern.length - 1) {
                    return true;
                } else {
                    c2 = pattern.charAt(pIndex + 1);
                    // 如果后面的字符和当前c1的字符一致，那么需要跳过*了
                    if (c1 == c2) {
                        pIndex += 2;
                    }
                }
                continue;
            } else {
                pIndex++;
                return false;
            }
        }
        pIndex++;
    }
    return true;
}


const arr = [1 << 10];
const arr2 = ["KB", "MB", "GB"]
for (let i = 1; i <= 2; i++) {
    arr[i] = arr[i - 1] << 10;
}
arr.reverse();
arr2.reverse();

/**
 * 获取文件大小显示
 * @param num 
 * @returns 
 */
const getByteSize = (num:number) => {
    if (num < 1024) {
        return num + "字节";
    }
    for (const i in arr) {
        const r = num / arr[i];
        if (r > 1) {
            return r + arr2[i];
        }
    }
}

const clearProperties = (obj:any) => {
    for (const key in obj) {
        delete obj[key];
    }
}

const getPathURL = (path:string) => {
    if (!path) {
        return "https://www.myinfomation.com/static/yy/image-error.png";
    }
    if (path.startsWith("https://zy.forerunnercollege.com")) {
        return path.replace("https://","http://");
    }
    if (path.startsWith("https://") || path.startsWith("http://")) {
        return path;
    }
    return "https://zy.myinfomation.com" + path;
    // return "https://zy.forerunnercollege.com" + path;
}

export {checkMatches,getByteSize,clearProperties,getPathURL}